﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Lottery.App.Utility
{
    public class Utils
    {
        public const string FORMAT_N1 = "{0:D2}";
        public const string FORMAT_N2 = "{0:D2} {1:D2}";
        public const string FORMAT_N3 = "{0:D2} {1:D2} {2:D2}";
        public const string FORMAT_N4 = "{0:D2} {1:D2} {2:D2} {3:D2}";
        public const string FORMAT_N5 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2}";
        public const string FORMAT_N6 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2}";
        public const string FORMAT_N7 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2}";
        public const string FORMAT_N8 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2} {7:D2}";



        /// <summary>
        /// 将Excel的列索引转换为列名，列索引从0开始，列名从A开始。如第0列为A，第1列为B...
        /// </summary>
        /// <param name="index">列索引</param>
        /// <returns>列名，如第0列为A，第1列为B...</returns>
        public static string ConvertColumnIndexToColumnName(int index)
        {
            index = index + 1;
            int system = 26;
            char[] digArray = new char[100];
            int i = 0;
            while (index > 0)
            {
                int mod = index % system;
                if (mod == 0) mod = system;
                digArray[i++] = (char)(mod - 1 + 'A');
                index = (index - 1) / 26;
            }
            StringBuilder sb = new StringBuilder(i);
            for (int j = i - 1; j >= 0; j--)
            {
                sb.Append(digArray[j]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 连二直(7种组合)
        /// </summary>
        public static List<string> GetA2(string n8)
        {
            List<string> list = new List<string>(7);

            string[] items = n8.Split(' ');

            list.Add(String.Format(FORMAT_N2, items[0], items[1]));
            list.Add(String.Format(FORMAT_N2, items[1], items[2]));
            list.Add(String.Format(FORMAT_N2, items[2], items[3]));
            list.Add(String.Format(FORMAT_N2, items[3], items[4]));
            list.Add(String.Format(FORMAT_N2, items[4], items[5]));
            list.Add(String.Format(FORMAT_N2, items[5], items[6]));
            list.Add(String.Format(FORMAT_N2, items[6], items[7]));

            return list;
        }

        /// <summary>
        /// 连二组(14种组合)
        /// </summary>
        public static List<string> GetP2(string n8)
        {
            List<string> list = new List<string>(14);

            string[] items = n8.Split(' ');

            list.Add(String.Format(FORMAT_N2, items[0], items[1]));
            list.Add(String.Format(FORMAT_N2, items[1], items[2]));
            list.Add(String.Format(FORMAT_N2, items[2], items[3]));
            list.Add(String.Format(FORMAT_N2, items[3], items[4]));
            list.Add(String.Format(FORMAT_N2, items[4], items[5]));
            list.Add(String.Format(FORMAT_N2, items[5], items[6]));
            list.Add(String.Format(FORMAT_N2, items[6], items[7]));

            list.Add(String.Format(FORMAT_N2, items[1], items[0]));
            list.Add(String.Format(FORMAT_N2, items[2], items[1]));
            list.Add(String.Format(FORMAT_N2, items[3], items[2]));
            list.Add(String.Format(FORMAT_N2, items[4], items[3]));
            list.Add(String.Format(FORMAT_N2, items[5], items[4]));
            list.Add(String.Format(FORMAT_N2, items[6], items[5]));
            list.Add(String.Format(FORMAT_N2, items[7], items[6]));

            return list;
        }

        /// <summary>
        /// 开奖8个号码里面5中4的组合(896种组合)
        /// </summary>
        public static List<string> Get5Z4(string n8)
        {
            IList<string> list = Get8P4(n8);

            List<string> dict = new List<string>(896); //70 * 16 = 1120 其中重复的有224注

            string value = null;

            foreach (string item in list)
            {
                for (int i = 1; i <= 20; i++)
                {
                    value = String.Format("{0:D2}", i);

                    if (item.Contains(value))
                        continue;

                    value = String.Format("{0} {1}", item, value);

                    value = Sort(value);

                    dict.Add(value);
                }
            }

            return dict;
        }

        /// <summary>
        /// 7选3(35种组合)
        /// </summary>
        public static List<string> Get7P3(string n7)
        {
            string[] array = n7.Split(' ');

            List<string> list = new List<string>(35);

            for (int i = 0; i < 5; i++)
            {
                for (int j = i + 1; j < 6; j++)
                {
                    for (int k = j + 1; k < 7; k++)
                    {
                        list.Add(String.Format(FORMAT_N3, array[i], array[j], array[k]));
                    }
                }
            }

            return list;
        }

        #region 复式统计和胆拖统计

        /// <summary>
        /// 一胆二拖买任选二(3420种组合)
        /// </summary>
        public static List<string> GetD1T2()
        {
            const string numbers = "01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20";

            List<string> list = new List<string>(3420);

            for (int num = 1; num <= 20; num++)
            {
                string str = numbers.Replace(num.ToString("D2"), "").Replace("  ", " ").Trim();

                int[] array = ConvertStringToIntArray(str);
                for (int i = 0; i <= 17; i++)
                {
                    for (int j = i + 1; j <= 18; j++)
                    {
                        list.Add(String.Format(FORMAT_N3, num, array[i], array[j]));
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 4选3(4种组合)
        /// </summary>
        public static List<string> Get4P3(string n4)
        {
            string[] items = n4.Split(' ');

            List<string> list = new List<string>(4);

            list.Add(String.Format(FORMAT_N3, items[0], items[1], items[2]));
            list.Add(String.Format(FORMAT_N3, items[0], items[1], items[3]));
            list.Add(String.Format(FORMAT_N3, items[0], items[2], items[3]));
            list.Add(String.Format(FORMAT_N3, items[1], items[2], items[3]));

            return list;
        }

        /// <summary>
        /// 5选4(5种组合)
        /// </summary>
        public static List<string> Get5P4(string n5)
        {
            string[] items = n5.Split(' ');

            List<string> list = new List<string>(5);

            list.Add(String.Format(FORMAT_N4, items[0], items[1], items[2], items[3]));
            list.Add(String.Format(FORMAT_N4, items[0], items[1], items[2], items[4]));
            list.Add(String.Format(FORMAT_N4, items[0], items[1], items[3], items[4]));
            list.Add(String.Format(FORMAT_N4, items[0], items[2], items[3], items[4]));
            list.Add(String.Format(FORMAT_N4, items[1], items[2], items[3], items[4]));

            return list;
        }

        /// <summary>
        /// 6选5(6种组合)
        /// </summary>
        public static List<string> Get6P5(string n6)
        {
            string[] items = n6.Split(' ');

            List<string> list = new List<string>(6);

            list.Add(String.Format(FORMAT_N5, items[0], items[1], items[2], items[3], items[4]));
            list.Add(String.Format(FORMAT_N5, items[0], items[1], items[2], items[3], items[5]));
            list.Add(String.Format(FORMAT_N5, items[0], items[1], items[2], items[4], items[5]));
            list.Add(String.Format(FORMAT_N5, items[0], items[1], items[3], items[4], items[5]));
            list.Add(String.Format(FORMAT_N5, items[0], items[2], items[3], items[4], items[5]));
            list.Add(String.Format(FORMAT_N5, items[1], items[2], items[3], items[4], items[5]));

            return list;
        }

        #endregion

        #region 8个开奖号码组合,用于获取各类玩法中奖集合
        /// <summary>
        /// 8选2(28种组合)
        /// </summary>
        public static List<string> Get8P2(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(28);

            for (int i = 0; i <= 6; i++)
            {
                for (int j = i + 1; j <= 7; j++)
                {
                    list.Add(String.Format(FORMAT_N2, items[i], items[j]));
                }
            }

            return list;
        }

        /// <summary>
        /// 8选3(56种组合)
        /// </summary>
        public static List<string> Get8P3(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(56);

            for (int i = 0; i <= 5; i++)
            {
                for (int j = i + 1; j <= 6; j++)
                {
                    for (int k = j + 1; k <= 7; k++)
                    {
                        list.Add(String.Format(FORMAT_N3, items[i], items[j], items[k]));
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 8选4(70种组合)
        /// </summary>
        public static List<string> Get8P4(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(70);

            for (int i = 0; i <= 4; i++)
            {
                for (int j = i + 1; j <= 5; j++)
                {
                    for (int k = j + 1; k <= 6; k++)
                    {
                        for (int m = k + 1; m <= 7; m++)
                        {
                            list.Add(String.Format(FORMAT_N4, items[i], items[j], items[k], items[m]));
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 8选5(56种组合)
        /// </summary>
        public static List<string> Get8P5(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(56);

            for (int i = 0; i <= 3; i++)
            {
                for (int j = i + 1; j <= 4; j++)
                {
                    for (int k = j + 1; k <= 5; k++)
                    {
                        for (int m = k + 1; m <= 6; m++)
                        {
                            for (int n = m + 1; n <= 7; n++)
                            {
                                list.Add(String.Format(FORMAT_N5, items[i], items[j], items[k], items[m], items[n]));
                            }
                        }
                    }
                }
            }

            return list;
        }

        #endregion

        #region 20个号码组合,用于初始化数据库

        /// <summary>
        /// 20选1(20种组合)
        /// </summary>
        public static List<string> Get20P1()
        {
            List<string> list = new List<string>(20);

            for (int i = 1; i <= 20; i++)
            {
                list.Add(String.Format(FORMAT_N1, i));
            }

            return list;
        }

        /// <summary>
        /// 20选2(190种组合)
        /// </summary>
        public static List<string> Get20P2()
        {
            List<string> list = new List<string>(190);

            for (int i = 1; i <= 19; i++)
            {
                for (int j = i + 1; j <= 20; j++)
                {
                    list.Add(String.Format(FORMAT_N2, i, j));
                }
            }

            return list;
        }

        /// <summary>
        /// 20选3(1140种组合)
        /// </summary>
        public static List<string> Get20P3()
        {
            List<string> list = new List<string>(1140);

            for (int i = 1; i <= 18; i++)
            {
                for (int j = i + 1; j <= 19; j++)
                {
                    for (int k = j + 1; k <= 20; k++)
                    {
                        list.Add(String.Format(FORMAT_N3, i, j, k));
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 20选4(4845种组合)
        /// </summary>
        public static List<string> Get20P4()
        {
            List<string> list = new List<string>(4845);

            for (int i = 1; i <= 17; i++)
            {
                for (int j = i + 1; j <= 18; j++)
                {
                    for (int k = j + 1; k <= 19; k++)
                    {
                        for (int m = k + 1; m <= 20; m++)
                        {
                            list.Add(String.Format(FORMAT_N4, i, j, k, m));
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 20选5(15504种组合)
        /// </summary>
        /// <returns></returns>
        public static List<string> Get20P5()
        {
            List<string> list = new List<string>(15504);

            for (int i = 1; i <= 16; i++)
            {
                for (int j = i + 1; j <= 17; j++)
                {
                    for (int k = j + 1; k <= 18; k++)
                    {
                        for (int m = k + 1; m <= 19; m++)
                        {
                            for (int n = m + 1; n <= 20; n++)
                            {
                                list.Add(String.Format(FORMAT_N5, i, j, k, m, n));
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 20选6(38760种组合)
        /// </summary>
        /// <returns></returns>
        public static List<string> Get20P6()
        {
            List<string> list = new List<string>(38760);

            for (int i = 1; i <= 15; i++)
            {
                for (int j = i + 1; j <= 16; j++)
                {
                    for (int k = j + 1; k <= 17; k++)
                    {
                        for (int m = k + 1; m <= 18; m++)
                        {
                            for (int n = m + 1; n <= 19; n++)
                            {
                                for (int x = n + 1; x <= 20; x++)
                                {
                                    list.Add(String.Format(FORMAT_N6, i, j, k, m, n, x));
                                }
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 20选7(77520种组合)
        /// </summary>
        /// <returns></returns>
        public static List<string> Get20P7()
        {
            List<string> list = new List<string>(77520);

            for (int y = 1; y <= 14; y++)
            {
                for (int i = y + 1; i <= 15; i++)
                {
                    for (int j = i + 1; j <= 16; j++)
                    {
                        for (int k = j + 1; k <= 17; k++)
                        {
                            for (int m = k + 1; m <= 18; m++)
                            {
                                for (int n = m + 1; n <= 19; n++)
                                {
                                    for (int x = n + 1; x <= 20; x++)
                                    {
                                        list.Add(String.Format(FORMAT_N7, y, i, j, k, m, n, x));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 20排列2(380种组合)
        /// </summary>
        public static List<string> Get20A2()
        {
            List<string> list = new List<string>(380);

            for (int i = 1; i <= 19; i++)
            {
                for (int j = i + 1; j <= 20; j++)
                {
                    list.Add(String.Format(FORMAT_N2, i, j));
                    list.Add(String.Format(FORMAT_N2, j, i));
                }
            }

            list.Sort();

            return list;
        }

        /// <summary>
        /// 20排列3(6840种组合)
        /// </summary>
        public static List<string> Get20A3()
        {
            List<string> list = new List<string>(6840);

            for (int i = 1; i <= 20; i++)
            {
                for (int j = 1; j <= 20; j++)
                {
                    if (i == j) continue;

                    for (int k = 1; k <= 20; k++)
                    {
                        if (i == k || j == k) continue;

                        list.Add(String.Format(FORMAT_N3, i, j, k));
                    }
                }
            }

            return list;
        }

        #endregion


        public static string Sort(string result)
        {
            object[] array = Array.ConvertAll(result.Split(' '), value => (object)value);

            Array.Sort<object>(array);

            switch (result.Split(' ').Length)
            {
                case 1: return String.Format(FORMAT_N1, array);
                case 2: return String.Format(FORMAT_N2, array);
                case 3: return String.Format(FORMAT_N3, array);
                case 4: return String.Format(FORMAT_N4, array);
                case 5: return String.Format(FORMAT_N5, array);
                case 6: return String.Format(FORMAT_N6, array);
                case 7: return String.Format(FORMAT_N7, array);
                case 8: return String.Format(FORMAT_N8, array);
                default: return result;
            }
        }

        public static int[] ConvertStringToIntArray(string value)
        {
            return Array.ConvertAll(value.Split(' '), val => Convert.ToInt32(val));
        }
    }
}
